<template>
  <div class="video-tab">
    <div class="tab">视频管理</div>
    <div class="search">
      <el-input
        clearable
        placeholder="搜索稿件"
        size="small"
        v-model="videoNameFuzzy"
        @keyup.enter="loadVideoList"
      >
        <template #suffix>
          <span class="iconfont icon-search"></span>
        </template>
      </el-input>
    </div>
  </div>
  <div class="video-manage">
    <div class="top-info">
      <div class="all-video-panel">
        <div class="all-video" @click="cleanStatusLoad">
          全部稿件<span class="count-info">{{
            countInfo.inProgress + countInfo.auditFailCount + countInfo.auditPassCount
          }}</span>
        </div>
      </div>
      <div class="video-status">
        <span :class="['item', status == -1 ? 'active' : '']" @click="statusLoad(-1)"
          >进行中<span class="count-info">{{ countInfo.inProgress }}</span></span
        >
        <el-divider direction="vertical"></el-divider>
        <span :class="['item', status == 3 ? 'active' : '']" @click="statusLoad(3)"
          >已通过<span class="count-info">{{ countInfo.auditPassCount }}</span></span
        >
        <el-divider direction="vertical"></el-divider>
        <span :class="['item', status == 4 ? 'active' : '']" @click="statusLoad(4)"
          >未通过<span class="count-info">{{ countInfo.auditFailCount }}</span></span
        >
      </div>
    </div>
    <div class="video-list">
      <DataList :dataSource="dataSource" @loadData="loadVideoList">
        <template #default="{ data }">
          <VideoItem :data="data" @reload="loadVideoList"></VideoItem>
        </template>
      </DataList>
    </div>
  </div>
</template>

<script setup lang="ts">
import {
  ucenterLoadVideoListHttpRequest,
  ucenterVideoStatusCountHttpRequest
} from '@/services/api/ucenter'
import VideoItem from './VideoItem.vue'
import { onMounted, onUnmounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const router = useRouter()
const route = useRoute()

const videoNameFuzzy = ref<string>()
const status = ref<number | null>()
const dataSource = ref<any>({ list: [] })
const countInfo = ref({
  inProgress: 0,
  auditPassCount: 0,
  auditFailCount: 0
})
// 获取状态视频的数量
const loadCountInfo = async () => {
  const res = await ucenterVideoStatusCountHttpRequest()
  countInfo.value = res.data
}
loadCountInfo()

//轮询获取视频的状态
let timer = ref<any>(null)
const startTimer = () => {
  timer.value = setInterval(() => {
    loadVideoList()
    loadCountInfo()
  }, 5000)
}

const cleanTimer = () => {
  if (timer.value != null) {
    clearInterval(timer.value)
    timer.value = null
  }
}
onMounted(() => {
  startTimer()
  loadVideoList()
})
onUnmounted(() => {
  cleanTimer()
})

const statusLoad = (_status: number) => {
  status.value = _status
  loadVideoList()
}

//获取稿件列表信息
const loadVideoList = async () => {
  let params = {
    pageNo: dataSource.value.pageNo as number,
    videoNameFuzzy: videoNameFuzzy.value as string,
    status: status.value as number
  }
  const res = await ucenterLoadVideoListHttpRequest(params)
  console.log(res, '稿件数据')
  if (!res) {
    return
  }
  dataSource.value = res.data
}

const cleanStatusLoad = () => {
  status.value = null
  loadVideoList()
}
</script>

<style lang="less" scoped>
.video-tab {
  border-bottom: 1px solid #ddd;
  display: flex;
  padding: 0 40px;
  justify-content: space-between;
  .tab {
    cursor: pointer;
    font-weight: bold;
    color: var(--blue);
    padding-bottom: 15px;
    border-bottom: 3px solid var(--blue);
  }
  .search {
    width: 200px;
  }
}
.video-manage {
  margin-top: 10px;
  padding: 0 40px 10px 40px;
  .top-info {
    .count-info {
      padding: 0 5px;
    }
    .all-video-panel {
      display: flex;
      .all-video {
        cursor: pointer;
        font-size: 14px;
        color: var(--blue);
      }
    }
    .video-status {
      margin-top: 10px;
      display: flex;
      align-items: center;
      .item {
        cursor: pointer;
        font-size: 13px;
        margin-right: 0px;
        color: var(--text2);
        &:hover {
          color: var(--blue);
        }
      }
      .active {
        color: var(--blue);
      }
    }
  }
}
</style>
